//
//  ST_BTBarrageInfoCell.swift
//  BTVideo
//
//  Created by DouLei on 2019/8/5.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import HandyJSON

class ST_BTBarrageInfoCell: UITableViewCell {

    
    //    MARK: - init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//        self.selectionStyle = .none
        
        self.tintColor = Color.init(hexString: "#42CEDA")
        
        self.contentView.addSubview(userImageBtn)
        userImageBtn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.top.equalToSuperview().offset(11)
            make.width.height.equalTo(60)
        }
        
        self.contentView.addSubview(content_label)
        self.contentView.addSubview(videoImageBtn)
        self.contentView.addSubview(nickname_label)
        self.contentView.addSubview(time_label)
        
        
        videoImageBtn.snp.makeConstraints { (make) in
            make.left.equalTo(userImageBtn.snp.right).offset(ScreenWidth-14-50-60-18)
            make.top.equalToSuperview().offset(15)
            make.width.height.equalTo(50)
        }
        

        nickname_label.snp.makeConstraints { (make) in
            make.left.equalTo(userImageBtn.snp.right).offset(15)
            make.top.equalToSuperview().offset(11)
        }


        content_label.snp.makeConstraints { (make) in
            make.left.equalTo(nickname_label)
            make.top.equalTo(nickname_label.snp.bottom).offset(5)
            make.right.equalTo(videoImageBtn.snp.left).offset(-14)
        }


        time_label.snp.makeConstraints { (make) in
            make.left.equalTo(nickname_label)
            make.top.equalTo(content_label.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-15).priority(999)
        }
        
        let line = UIView()
        line.backgroundColor = Color.init(hexString: "#F2F4F7")
        self.contentView.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let selectBackView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.bounds.width, height: self.bounds.height))
        selectBackView.backgroundColor = Color.clear
        self.selectedBackgroundView = selectBackView
        
        change_edit_control_color()
    }
    
    override func didTransition(to state: UITableViewCell.StateMask) {
        if state == UITableViewCell.StateMask.showingEditControl{
            change_edit_control_color()
        }
    }
  
    
    
    func change_edit_control_color(){



        for child in self.subviews{

//            for child in item.subviews{

                if child.isKind(of: NSClassFromString("UITableViewCellEditControl")!) {

                        if let imageV = child.value(forKey: "_imageView") as? UIImageView{
                            
                            imageV.image = self.isSelected ? selectMarkImage : markImage
                        }
                }
//            }
        }

    }
    
    
//    MARK: - Properties
    fileprivate lazy var markImage = ST_ImageUtils.imageName("弹幕未选择")!
    
    fileprivate lazy var selectMarkImage = ST_ImageUtils.imageName("弹幕选择")!
    
    lazy var userImageBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
//        control.backgroundColor = Color.white
        control.cornerRadius = 60/2.0
        control.borderWidth = 2
        control.borderColor = Color.init(hex: 0xF2F4F7)
        control.imageView?.contentMode = UIView.ContentMode.scaleAspectFill
//        control.isUserInteractionEnabled = false
        return control
    }()
    
    lazy var click_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.add_touch_up(insideTarget: self, action: #selector(click_handle))
        return control
    }()

    lazy var videoImageBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.white
        control.cornerRadius = 5
        control.imageView?.contentMode = UIView.ContentMode.scaleAspectFill

        return control
    }()

    
    
    
    lazy var nickname_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        return control
    }()

    
    
    
    lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#666666")
        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()

    
    
    
    lazy var time_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#999999")
//        control.numberOfLines = 0
//        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()

//    MARK: - Bind Model
    var model = ST_BTBarrageInfoCellModel(){
        didSet{
            
            userImageBtn.sd_setAllImage(with: model.avatar)
            
            videoImageBtn.sd_setAllImage(with: model.cover_img)
            
            nickname_label.text = model.nickname
            
            content_label.text = model.reply_content
            
            time_label.text = (model.create_time.double() ?? 0).b_t_global_date_string()
        }
    }
    
//    MARK: - Event
    @objc func click_handle(){
        
        
    }
    
}


class ST_BTBarrageInfoCellModel: NSObject,HandyJSON {
    
    var id = ""
    var nickname = ""
    var avatar = ""
    var reply_content = ""
    var create_time = ""
    var cover_img = ""
    var video_id = ""
    var member_accid = ""
    required override init() {
        super.init()
    }
}
